#!/bin/bash
set -ex


_help () {
echo "Copyright (c) 2020 Luet Extensions

luet apkbuildtospec [opts]

Converts a remote APKBUILD url to a luet spec

Example:

  $> luet apkbuildtospec https://..../APKBUILD newcategory

"
return 0
}

if [ $# -eq 0 ] || [ "$1" == "--help" ] || [ "$1" == "-h" ] || [ "$1" == "help" ]; then
    _help
    exit 1
fi

APKBUILD=$1

CATEGORY=${2:-stub}

b="$(curl -L -s $APKBUILD)"
eval "$b"

mkdir -p $pkgname || true
touch $pkgname/definition.yaml || true

# Add 1 to a collection
yq w -i $pkgname/definition.yaml "name" $pkgname
yq w -i $pkgname/definition.yaml "version" $pkgver --style double
yq w -i $pkgname/definition.yaml "category" "$CATEGORY"
yq w -i $pkgname/definition.yaml "description" "$pkgdesc"
yq w -i $pkgname/definition.yaml "uri[0]" "$url"
yq w -i $pkgname/definition.yaml "license" "$license"

readarray -t s <<<"$source"

yq w -i $pkgname/definition.yaml "labels.\"package.source\"" ${s[0]}
echo "$b" > $pkgname/build.sh


touch $pkgname/build.yaml || true

yq w -i $pkgname/build.yaml "image" "quay.io/mocaccino/micro-toolchain:latest"
yq w -i $pkgname/build.yaml "env[0]" 'CFLAGS="-g -O2 -I/include -I/usr/include -I/usr/include/X11 -static"'
yq w -i $pkgname/build.yaml "env[1]" 'CPPFLAGS="-I/include -I/usr/include -I/usr/include/X11 -static"'
yq w -i $pkgname/build.yaml "env[2]" 'LUET_YES="true"'

# Generate steps
yq w -i $pkgname/build.yaml "prelude[0]" "luet install libs/zlib shells/bash utils/autoconf utils/curl libs/zlib vcs/git development/automake development/pkg-config development/m4 lang/python \
   development/make system/musl-toolchain"

strip_source=$( echo ${s[0]} | sed -E 's,\\t|\\r|\\n,,g')
yq w -i $pkgname/build.yaml "prelude[1]" "mkdir -p source && curl -L $strip_source | tar xJvf - -C source"

next=2
if [ -n "$builddir" ]; then
    yq w -i $pkgname/build.yaml "prelude[$next]" "mv source${builddir} $builddir" --style double
    next=3
fi

export pkgdir=/package
yq w -i $pkgname/build.yaml "prelude[$next]" "export \$pkgdir=/package"
    next=$((next+1))

if [ -n "$builddir" ]; then
yq w -i $pkgname/build.yaml "prelude[$next]" "export \$srcdir=/$builddir"
else 
yq w -i $pkgname/build.yaml "prelude[$next]" "export \$srcdir=/luetbuild/source"
fi
if [ -n "$pkgdir" ]; then
    next=$((next+1))

    yq w -i $pkgname/build.yaml "prelude[$next]" "mkdir -p $pkgdir"
    yq w -i $pkgname/build.yaml "package_dir" "$pkgdir"
fi
    next=$((next+1))

yq w -i $pkgname/build.yaml "prelude[$next]" "/bin/bash -cx \"source build.sh && cd source && build\""
yq w -i $pkgname/build.yaml "steps[0]" "/bin/bash -ce \"source build.sh && cd source && package\""

# Generate depends
u=0
for i in $makedepends; do
    yq w -i $pkgname/build.yaml "requires[$u].category" "$CATEGORY"
    yq w -i $pkgname/build.yaml "requires[$u].name" "$i"
    yq w -i $pkgname/build.yaml "requires[$u].version" ">=0"
    u=$((u+1))
done

u=0
for i in $depends; do
    yq w -i $pkgname/definition.yaml "requires[$u].category" "$CATEGORY"
    yq w -i $pkgname/definition.yaml "requires[$u].name" "$i"
    yq w -i $pkgname/definition.yaml "requires[$u].version" ">=0"
    u=$((u+1))
done


